package com.yanchangapp.ui.activity.mail;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.util.PerfectClickListener;
import com.yanchangapp.R;
import com.yanchangapp.factory.MailHomeFactory;
import com.yanchangapp.ui.fragment.mail.MailContactFragment;
import com.yanchangapp.ui.fragment.mail.MailListFragment;
import com.yanchangapp.ui.presenter.MailHomeAtPresenter;
import com.yanchangapp.ui.view.MailHomeAtView;
import com.zxl.baselib.util.ui.ResHelper;
import com.zxl.baselib.util.ui.UIUtils;
import com.zxl.baselib.util.window.DisplayHelper;

import java.util.List;

import butterknife.BindView;


public class MailHomeActivity extends BaseActivity<MailHomeAtView, MailHomeAtPresenter> implements MailHomeAtView {
    @BindView(R.id.mailHomeFrame)
    FrameLayout mailHomeFrame;
    @BindView(R.id.rbMailList)
    RadioButton rbMailList;
    @BindView(R.id.rbContact)
    RadioButton rbContact;
    @BindView(R.id.rpMailHome)
    RadioGroup rpMailHome;
    @BindView(R.id.linearBottom)
    LinearLayout linearBottom;
    @BindView(R.id.rlWriteMail)
    RelativeLayout rlWriteMail;
    private FragmentManager mSupportFragmentManager;
    private MailContactFragment mMailContactFragment;
    private MailListFragment mMailListFragment;
    private boolean isEditNow = false;

    public static void show(Activity activity){
        Intent intent = new Intent(activity,MailHomeActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void init() {
        mMailContactFragment = null;
        mMailListFragment = null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_mail_home;
    }

    @Override
    protected MailHomeAtPresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        //设置底部导航栏颜色
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            getWindow().setNavigationBarColor(ContextCompat.getColor(this, R.color.white));
//        }
        initRadioButton();
        clearOldFragment();
        rbMailList.setChecked(true);
        showWhichFragment(0);
    }

    private void initRadioButton() {

        Drawable dbHome = getResources().getDrawable(R.drawable.selector_bottom_mail_mail);
        dbHome.setBounds(0, 0, DisplayHelper.dp2Px(25), DisplayHelper.dp2Px(25));
        rbMailList.setCompoundDrawables(null, dbHome, null, null);

        Drawable dbPond = getResources().getDrawable(R.drawable.selector_bottom_mail_contact);
        dbPond.setBounds(0, 0, DisplayHelper.dp2Px(25), DisplayHelper.dp2Px(25));
        rbContact.setCompoundDrawables(null, dbPond, null, null);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        rpMailHome.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId){
                case R.id.rbMailList:
                    showWhichFragment(0);
                    break;
                case R.id.rbContact:
                    showWhichFragment(1);
                    break;
            }
        });

        rlWriteMail.setOnClickListener(new PerfectClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                MailWriteActivity.show((MailHomeActivity)mContext);
            }
        });
    }

    public void showHiddenButton(boolean isEditType) {
        isEditNow = isEditType;
        if (isEditType){
            linearBottom.setVisibility(View.GONE);
        }else {
            linearBottom.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            if (isEditNow){
                if (mMailListFragment != null){
                    isEditNow = false;
                    linearBottom.setVisibility(View.VISIBLE);
                    mMailListFragment.setLookState();
                }
                return false;
            }
        }
        return super.onKeyDown(keyCode, event);

    }

    private void clearOldFragment() {
        if (mSupportFragmentManager == null){
            mSupportFragmentManager = getSupportFragmentManager();
        }
        FragmentTransaction transaction = mSupportFragmentManager.beginTransaction();
        List<Fragment> fragments = mSupportFragmentManager.getFragments();
        if (transaction == null || fragments == null || fragments.size() == 0) {
            return;
        }
        boolean doCommit = false;
        for (Fragment fragment : fragments) {
            if (fragment != null) {
                transaction.remove(fragment);
                doCommit = true;
            }
        }
        if (doCommit) {
            transaction.commitNow();
        }
    }

    private void  showWhichFragment(int index){
        FragmentTransaction fragmentTransaction = mSupportFragmentManager.beginTransaction();
        hideAllFragment(fragmentTransaction);
        switch (index){
            case 0:
                if (mMailListFragment == null){
                    mMailListFragment = new MailListFragment();
                    fragmentTransaction.add(R.id.mailHomeFrame,mMailListFragment);
                }else {
                    fragmentTransaction.show(mMailListFragment);
                }
                break;
            case 1:
                if (mMailContactFragment == null){
                    mMailContactFragment = new MailContactFragment();
                    fragmentTransaction.add(R.id.mailHomeFrame,mMailContactFragment);
                }else {
                    fragmentTransaction.show(mMailContactFragment);
                }
                break;
            default:
                break;
        }
        fragmentTransaction.commit();
    }

    private void  hideAllFragment(FragmentTransaction fragmentTransaction){
        if (mMailListFragment != null){
            fragmentTransaction.hide(mMailListFragment);
        }

        if (mMailContactFragment != null){
            fragmentTransaction.hide(mMailContactFragment);
        }
    }

}
